﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime.Functions;
using System.Collections;
using System.Globalization;

namespace Xacml.Core.Runtime.DataTypes
{
  public class X500Name : IDataType
  {
    private ArrayList _keys = new ArrayList();
    private Hashtable _parts = new Hashtable();
    private string _name;

    public IFunction EqualFunction
    {
      get
      {
        return (IFunction) new X500NameEqual();
      }
    }

    public IFunction IsInFunction
    {
      get
      {
        return (IFunction) new X500NameIsIn();
      }
    }

    public IFunction SubsetFunction
    {
      get
      {
        return (IFunction) new X500NameSubset();
      }
    }

    public string DataTypeName
    {
      get
      {
        return "urn:oasis:names:tc:xacml:1.0:data-type:x500Name";
      }
    }

    internal X500Name()
    {
    }

    public X500Name(string name)
    {
      this._name = name;
      string str1 = name;
      char[] chArray1 = new char[1]
      {
        ','
      };
      foreach (string str2 in str1.Split(chArray1))
      {
        char[] chArray2 = new char[1]
        {
          '='
        };
        string[] strArray = str2.Split(chArray2);
        this._keys.Add((object) strArray[0].Trim());
        this._parts.Add((object) strArray[0].Trim(), (object) strArray[1].Trim());
      }
    }

    public override int GetHashCode()
    {
      return this._name.GetHashCode();
    }

    public override bool Equals(object obj)
    {
      if (!(obj is X500Name))
        return base.Equals(obj);
      X500Name x500Name = (X500Name) obj;
      for (int index1 = 0; index1 < this._keys.Count; ++index1)
      {
        bool flag = false;
        for (int index2 = 0; index2 < x500Name._keys.Count; ++index2)
        {
          if (string.Compare((string) this._keys[index1], (string) x500Name._keys[index2], true, CultureInfo.InvariantCulture) == 0)
          {
            flag = true;
            if (string.Compare((string) this._parts[this._keys[index1]], (string) x500Name._parts[x500Name._keys[index2]], false, CultureInfo.InvariantCulture) != 0)
              return false;
            break;
          }
        }
        if (!flag)
          return false;
      }
      for (int index1 = 0; index1 < x500Name._keys.Count; ++index1)
      {
        bool flag = false;
        for (int index2 = 0; index2 < this._keys.Count; ++index2)
        {
          if (string.Compare((string) x500Name._keys[index1], (string) this._keys[index2], true, CultureInfo.InvariantCulture) == 0)
          {
            flag = true;
            if (string.Compare((string) x500Name._parts[x500Name._keys[index1]], (string) this._parts[this._keys[index2]], false, CultureInfo.InvariantCulture) != 0)
              return false;
            break;
          }
        }
        if (!flag)
          return false;
      }
      return true;
    }

    public bool Matches(X500Name compareTo)
    {
      for (int index1 = 0; index1 < this._keys.Count; ++index1)
      {
        bool flag = false;
        for (int index2 = 0; index2 < compareTo._keys.Count; ++index2)
        {
          if (string.Compare((string) this._keys[index1], (string) compareTo._keys[index2], true, CultureInfo.InvariantCulture) == 0)
          {
            flag = true;
            if (string.Compare((string) this._parts[this._keys[index1]], (string) compareTo._parts[compareTo._keys[index2]], false, CultureInfo.InvariantCulture) != 0)
              return false;
            break;
          }
        }
        if (!flag)
          return false;
      }
      return true;
    }
  }
}
